#----- 1st Staging: golang compile -----
FROM golang:1.14.4-alpine AS builder
#@@@ arguments @@@
ARG GolangProject=apiserver
ARG GolangObj=apiserver
ARG GolangSrcEntry=PostFormWithoutToken.go

COPY ./*.go      build/$GolangProject/
COPY ./go.mod    build/$GolangProject/go.mod
COPY ./go.sum    build/$GolangProject/go.sum

# tencent alpine repository
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.cloud.tencent.com/g' /etc/apk/repositories;
ENV GO111MODULE on
ENV GOPROXY "https://goproxy.cn,https://goproxy.io,direct"
RUN apk update \
	&& apk add --no-cache git gcc musl-dev \
    && cd build/$GolangProject \
    && go mod download \
	&& go build -v -o /go/$GolangObj $GolangSrcEntry

#----- 2nd Staging: Target image -----
FROM alpine
#@@@ arguments @@@
ARG Service=apiserver
ARG ExecuteBinary=apiserver

MAINTAINER jinquan jinquan7@foxmail.com
LABEL Description="$Service version 1.0"
#@@@ copy files to container image @@@
COPY --from=builder /go/$ExecuteBinary /app/
#COPY ./data/config.ini /app/

#@@@ generate container internal entrypoint script @@@
# --conffsubdir=	    | Null means same as ExecuteBinary's directory
# --conffsubdir=../etc/dir 	   | A customised directory
RUN \
echo '#!/bin/sh' > /usr/sbin/entrypoint.sh; \
echo -n "/app/$ExecuteBinary --conffname=config.ini --conffsubdir=../etc/$Service >> " >> /usr/sbin/entrypoint.sh; \
echo -e "/app/data/$ExecuteBinary.log &" >> /usr/sbin/entrypoint.sh; \
echo -n 'tail -f /dev/null' >> /usr/sbin/entrypoint.sh;

RUN chmod +x /usr/sbin/entrypoint.sh

ENTRYPOINT ["/usr/sbin/entrypoint.sh"]
